﻿using LinkyPOS.Class;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinkyPOS
{
    public partial class admin : Form
    {
        public admin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // data trasnferi yapılıyor
            try
            {
                int musteriID;
                string isim = "";
                string soyisim = "";
                string mail = "";
                string telefon = "";


                // data transefer müşteri katmanı

                SqlConnection sqlcon = connection.baglanti;
                SqlConnection sqlcon2 = connection.baglanti2;


                SqlCommand sqlcmd = new SqlCommand("select * from musteriler", sqlcon2);

                sqlcon2.Open();

                SqlDataReader sdr = sqlcmd.ExecuteReader();
                while (sdr.Read())
                {

                    musteriID = (int)sdr["musteriID"];
                    isim = (string)sdr["isim"];
                    soyisim = (string)sdr["soyisim"];
                    mail = (string)sdr["mail"];
                    telefon = (string)sdr["telefon"];

                    SqlCommand sqlcmd2 = new SqlCommand("insert into musteriler (sistemID,isim,soyisim,eposta,telefon,yildiz) values(0,@isim,@soyisim,@mail,@telefon,5)", sqlcon);
                    sqlcmd2.Parameters.AddWithValue("@isim", isim);
                    sqlcmd2.Parameters.AddWithValue("@soyisim", soyisim);
                    sqlcmd2.Parameters.AddWithValue("@mail", mail);
                    sqlcmd2.Parameters.AddWithValue("@telefon", telefon);

                    sqlcon.Open();

                    sqlcmd2.ExecuteNonQuery();

                    sqlcon.Close();





                }

                sqlcon2.Close();

                int siparisID;
                string sipariskodu = "";
                musteriID = 0;
                string alimtipi = "";
                string teslimtipi = "";
                string alimtarihi = "";
                string alimsaati = "";
                string toplambrut = "";
                string iskontoorani = "";
                string iskontoreferans = "";
                string net = "";
                int tahsilattipi = 0;
                string odemetarihi = "";
                int drm = 0;



                // siparişler katmanı

                SqlCommand sqlcmd3 = new SqlCommand("select * from siparisler", sqlcon2);

                sqlcon2.Open();
                SqlDataReader sdr2 = sqlcmd3.ExecuteReader();
                while (sdr2.Read())
                {

                    sipariskodu = (string)sdr2["sipariskodu"];
                    musteriID = (int)sdr2["musteriID"];
                    alimtipi = (string)sdr2["alimtipi"];
                    teslimtipi = (string)sdr2["teslimtipi"];
                    DateTime dt = Convert.ToDateTime(sdr2["alimtarihi"]);
                    alimtarihi = dt.ToShortDateString();
                    string[] dizi = alimtarihi.Split('.');
                    alimtarihi=dizi[2]+"-"+dizi[1]+"-"+dizi[0];
                    alimsaati = sdr2["alimsaati"].ToString();
                    toplambrut = (string)sdr2["toplambrut"];
                    iskontoorani = (string)sdr2["iskontoorani"];
                    iskontoreferans = (string)sdr2["iskontoreferans"];
                    net = (string)sdr2["net"];
                    drm = (int)sdr2["durum"];
                    tahsilattipi = (int)sdr2["tahsilattipi"];
                    DateTime dt2= Convert.ToDateTime( sdr2["odemetarihi"]);
                    odemetarihi = dt2.ToShortDateString();
                    string[] dizi2=odemetarihi.Split('.');
                    odemetarihi = dizi2[2] + "-" + dizi2[1] + "-" + dizi2[0];

                    SqlCommand sqlcmd4 = new SqlCommand("insert into siparisler(sipariskodu,musteriID,teslimtipi,alimtipi,alimtarihi,alimsaati,toplambrut,iskonto,referans,net,durum,odemetarihi) values(@sipariskodu,@musteriID,@teslimtipi,@alimtipi,@alimtarihi,@alimsaati,@toplambrut,@iskonto,@referans,@net,@durum,@odemetarihi)", sqlcon);
                    sqlcmd4.Parameters.AddWithValue("@sipariskodu", sipariskodu);
                    sqlcmd4.Parameters.AddWithValue("@musteriID", musteriID);
                    if (teslimtipi == "Mağaza Teslim")
                    {
                        sqlcmd4.Parameters.AddWithValue("@teslimtipi", 1);
                    }
                    else
                    {
                        sqlcmd4.Parameters.AddWithValue("@teslimtipi", 0);
                    }
                    if (alimtipi == "Mağazadan Alınacak")
                    {
                        sqlcmd4.Parameters.AddWithValue("@alimtipi", 1);
                    }
                    else
                    {
                        sqlcmd4.Parameters.AddWithValue("@alimtipi", 0);
                    }

                    sqlcmd4.Parameters.AddWithValue("@alimtarihi", alimtarihi);
                    sqlcmd4.Parameters.AddWithValue("@alimsaati", alimsaati);
                    sqlcmd4.Parameters.AddWithValue("@toplambrut", toplambrut.Replace(',', '.'));
                    sqlcmd4.Parameters.AddWithValue("@iskonto", iskontoorani);
                    sqlcmd4.Parameters.AddWithValue("@referans", iskontoreferans);
                    sqlcmd4.Parameters.AddWithValue("@net", net.Replace(',','.'));
                    switch (tahsilattipi)
                    {
                        case 0:
                            if (drm==0)
                            {
                                sqlcmd4.Parameters.AddWithValue("@durum", 2);
                            }
                            else
                            {
                                sqlcmd4.Parameters.AddWithValue("@durum", 4);
                            }

                            break;
                        case 1:
                            if (drm == 0)
                            {
                                sqlcmd4.Parameters.AddWithValue("@durum", 3);
                            }
                            else
                            {
                                sqlcmd4.Parameters.AddWithValue("@durum", 5);
                            }
                            break;
                        case 2:

                            sqlcmd4.Parameters.AddWithValue("@durum", 6);

                            break;
                        case 3:
                            sqlcmd4.Parameters.AddWithValue("@durum", 7);
                            break;
                        case 4:

                            sqlcmd4.Parameters.AddWithValue("@durum", 19);

                            break;
                        case 5:

                            sqlcmd4.Parameters.AddWithValue("@durum", 1);

                            break;

                    }

                    sqlcmd4.Parameters.AddWithValue("@odemetarihi", odemetarihi);

                    sqlcon.Open();

                    sqlcmd4.ExecuteNonQuery();
                    sqlcon.Close();
                }

                sqlcon2.Close();



                // siparis detaylari katmanı

                int urunID = 0;
                string marka = "";
                string renk = "";
                string aciklama = "";



                SqlCommand sqlcmd5 = new SqlCommand("select * from siparisdetaylari", sqlcon2);

                sqlcon2.Open();
                SqlDataReader sdr3 = sqlcmd5.ExecuteReader();
                while (sdr3.Read())
                {
                    urunID = (int)sdr3["urunID"];
                    marka = (string)sdr3["marka"];
                    renk = (string)sdr3["renk"];
                    aciklama = (string)sdr3["aciklama"];
                    sipariskodu = (string)sdr3["sipariskodu"];
                    SqlCommand sqlcmd6 = new SqlCommand("insert into siparisdetaylari (teslimdurum,sipariskodu,urunID,marka,renk,aciklama,netfiyat,tip) values(@teslimdurumu,@sipariskodu,@urunID,@marka,@renk,@aciklama,@netfiyat,@tip)", sqlcon);

                    sqlcmd6.Parameters.AddWithValue("@teslimdurumu", 0);
                    sqlcmd6.Parameters.AddWithValue("@sipariskodu", sipariskodu);
                    switch (urunID)
                    {
                        case 1:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 16);
                            break;
                        case 2:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 2);
                            break;
                        case 3:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 3);
                            break;
                        case 4:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 4);
                            break;
                        case 5:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 8);
                            break;
                        case 6:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 10);
                            break;
                        case 7:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 11);
                            break;
                        case 8:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 6);
                            break;
                        case 9:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 15);
                            break;
                        case 10:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 13);
                            break;
                        case 11:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 18);
                            break;
                        case 12:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 19);
                            break;
                        case 13:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 12);
                            break;
                        case 14:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 20);
                            break;
                        case 15:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 22);
                            break;
                        case 16:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 23);
                            break;
                        case 17:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 21);
                            break;
                        case 18:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 28);
                            break;
                        case 19:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 5);
                            break;
                        case 20:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 26);
                            break;
                        case 21:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 24);
                            break;
                        case 22:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 14);
                            break;
                        default:
                            sqlcmd6.Parameters.AddWithValue("@urunID", 28);
                            break;

                    }

                    sqlcmd6.Parameters.AddWithValue("@marka", marka);
                    sqlcmd6.Parameters.AddWithValue("@renk", renk);
                    sqlcmd6.Parameters.AddWithValue("@aciklama", aciklama);
                    sqlcmd6.Parameters.AddWithValue("@netfiyat", 0);
                    sqlcmd6.Parameters.AddWithValue("@tip", "Kurutemizleme");


                    sqlcon.Open();

                    sqlcmd6.ExecuteNonQuery();
                    sqlcon.Close();



                }



                sqlcon2.Close();

                MessageBox.Show("BAŞARILI");
                this.Close();
            }
            catch (Exception d)
            {

                err2 er = new err2(d.ToString(), this.Name);
            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtsifre.Text == "admininetput")
            {
                panel1.Visible = false;
            }

            else
            {
                this.Close();
            }
        }

        private void admin_Load(object sender, EventArgs e)
        {

            try
            {

        
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("select top(1)err from err where formname='admin'",sqlcon);
            sqlcon.Open();
            SqlDataReader sdr = sqlcmd.ExecuteReader();
            while (sdr.Read())
            {
                richTextBox1.Text = sdr["err"].ToString();
            }
            sqlcon.Close();

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
